// 第一个能不能用promize的异步函数，封装一个数据库的查询 吧sequelize配置完成 至少能查询一个表去看看sequelize的文档

//引用
var fs = require('fs');
const { Sequelize, Model } = require('sequelize');
const { database, host, port, user, password } = require('./config').data;
const sequelize = new Sequelize(database, user, password, {
    dialect: 'mysql',
    host,
    port,
    //是否开启日志
    logging: true,
    timezone: '+08:00',//北京时间
    define: {
        timestamps: false,
        paranoid: true,
        underscored:true,
        scopes:{}
    }
});

//创建模型 ,不存在则创建
sequelize.sync({
    force: false
})
//模型名称和表名关联，int里面的属性跟字段关联
class usertable extends Model { }

usertable.init({
    id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        primaryKey: true
    },

    name: {
        type: Sequelize.STRING
    },
    age: {
        type: Sequelize.INTEGER
    },
    xin: {
        type: Sequelize.STRING
    }
}, {
    sequelize,
    //给对象取名字
    modelName: 'usertables',
    //绑定的表名
    tableName: 'usertable',
})

//使用promise 封装查询数据库

//resolve输出正确，refjec输出错误
new Promise(async function(resolve,refject){
    var one=await usertable.findAll({
        where:{
            id:'3'
        }
    })
    resolve(one);//吧正确的存进去
}).then(function(one) {  //正确输出
    console.log(one);
}).catch(function(err){ //报错提示
console.log(err);
})



